package com.xuanwei.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanwei.core.entity.vo.ProductListVo;
import com.xuanwei.core.entity.param.Navigate;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwHomepageNavigateService;
import com.xuanwei.dao.entity.XwHomepageNavigate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/7/31 10:55
 **/
@RestController
@RequestMapping("api/navigate")
@Validated
public class NavigateController extends BasicController {

    private IXwHomepageNavigateService navigateService;

    public NavigateController(IXwHomepageNavigateService navigateService) {
        this.navigateService = navigateService;
    }

    @RequestMapping(value = "add.do", method = RequestMethod.GET)
    public ResultVO add(@Valid Navigate navigate) {
        navigateService.addOrUpdate(navigate);
        return ResultVO.successResult("保存成功");
    }

    @RequestMapping(value = "updateStatus.do", method = RequestMethod.GET)
    public ResultVO updateStatus(@Min(value = 1, message = "ID格式错误") Integer id) {
        navigateService.changeStatus(id);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    public ResultVO delete(@Min(value = 1, message = "ID格式错误") Integer id) {
        navigateService.delete(id);
        return ResultVO.successResult("删除成功");
    }

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public ResultVO all() {
        List<XwHomepageNavigate> all = navigateService.all();
        return ResultVO.successResult(all);
    }

    @RequestMapping(value = "addProductToNavigate.do", method = RequestMethod.GET)
    public ResultVO addProductToNavigate(@Min(value = 1, message = "ID格式错误") Integer id,
                                         @NotBlank(message = "商品不能为空") String productIdsStr) {

        List<Integer> productIds = Arrays.stream(productIdsStr.split("-"))
                .map(Integer::parseInt).collect(Collectors.toList());

        navigateService.addProductToNavigate(id, productIds);
        return ResultVO.successResult("添加成功");
    }

    @RequestMapping(value = "productList.do", method = RequestMethod.GET)
    public ResultVO productList(@Min(value = 1, message = "ID格式错误") Integer id) {
        List<ProductListVo> productListVos = navigateService.findProductById(id);

        return ResultVO.successResult(productListVos);
    }

    @RequestMapping(value = "removeProduct.do", method = RequestMethod.GET)
    public ResultVO removeProduct(@Min(value = 1, message = "ID格式错误") Integer id) {
        navigateService.removeProduct(id);

        return ResultVO.successResult("移除成功");
    }

    @RequestMapping(value = "searchProduct.do", method = RequestMethod.GET)
    public ResultVO searchProduct(@Min(value = 1, message = "ID格式错误") Integer id, Integer productId, String name) {
        Page<ProductListVo> productListVoPage = navigateService.searchProductList(id, productId, name, getPage());

        return ResultVO.successResult(productListVoPage);
    }
}
